#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<map>
using namespace std;

// 构建显示数字到真实值的映射表
map<char, int> buildDigitMap() {
    map<char, int> digit_map;
    digit_map['0'] = 0;
    digit_map['1'] = 1;
    digit_map['2'] = 2;
    digit_map['4'] = 3;
    digit_map['5'] = 4;
    digit_map['6'] = 5;
    digit_map['7'] = 6;
    digit_map['9'] = 7;
    return digit_map;
}


long long convertToRealMileage(string& display,map<char, int>& digit_map){
	long long real_value=0;
	for(char ch:display){
		int digit=digit_map.at(ch);
		real_value=real_value*8+digit;	
	}//循环8进制
	return real_value;	
}
 

int main(){
	 // 构建映射表
    map<char, int> digit_map = buildDigitMap();
	int T;
	cin>>T;
	 cin.ignore(); // 忽略换行符
	while(T--){
		string ss;
		cin>>ss;
        // 转换为真实里程
        long long real_mileage = convertToRealMileage(ss, digit_map);
        // 输出结果
        cout << real_mileage << endl;
	}
	return 0;
}